package com.handpay.zztong.hp.swiper.ic;

public class StringUtil
{
	/**
	 * 字符串转16进制
	 * 
	 * @param str
	 * @return
	 */
	public static String string2Hex(String str)
	{
		char[] chars = str.toCharArray();
		StringBuffer hex = new StringBuffer();
		for (int i = 0; i < chars.length; i++)
		{
			hex.append(Integer.toHexString((int) chars[i]));
		}
		return hex.toString();
	}
	
	/**
	 * hex十六进制 转String
	 * @param hex
	 * @return
	 */
	public static String hex2String(String hex)
	{
		byte[] datas = hex2bytes(hex);
		return new String(datas);
	}
	
	/**
	 * 字节异或
	 * 
	 * @param src0
	 * @param src1
	 * @return
	 */
	public static byte byteXOR(byte src0, byte src1)
	{
		return (byte) ((src0 & 0xFF) ^ (src1 & 0xFF));
	}
	
	/**
	 * 字节数组异或
	 * 
	 * @param src0
	 * @param src1
	 * @return
	 */
	public static byte[] bytesXOR(byte[] src0, byte[] src1)
	{
		int length = src0.length;
		if (length != src1.length)
		{
			return null;
		}
		byte[] result = new byte[length];
		for (int i = 0; i < length; i++)
		{
			result[i] = byteXOR(src0[i], src1[i]);
		}
		return result;
	}
	
	/**
	 * byte二进�?�?hex十六进制
	 * 
	 * @param bytes
	 * @return
	 */
	public static String byte2hex(byte[] bytes)
	{
		if(bytes == null || bytes.length < 1)
		{
			return null;
		}
		StringBuilder retString = new StringBuilder();
		for (int i = 0; i < bytes.length; ++i)
		{
			retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF))
					.substring(1).toUpperCase());
		}
		return retString.toString();
	}
	
	public static String byte2ASCII(byte[] bytes)
	{
		if(bytes == null || bytes.length < 1)
		{
			return null;
		}
		StringBuilder retString = new StringBuilder();
		for (int i = 0; i < bytes.length; ++i)
		{
			retString.append((char)bytes[i]);
		}
		return retString.toString();
	}
	
	/**
	 * hex十六进制 �?byte二进�?
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] hex2bytes(String hex)
	{
		byte[] bts = new byte[hex.length() / 2];
		for (int i = 0; i < bts.length; i++)
		{
			bts[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2),
					16);
		}
		return bts;
	}
	
	/**
	 * hex十六进制 �?byte二进�?
	 * 
	 * @param hex
	 * @return
	 */
	public static Byte hex2byte(String hex)
	{
		byte[] bts = hex2bytes(hex);
		if (bts.length == 1)
		{
			return bts[0];
		}
		return Byte.MIN_VALUE;
	}
	
	/**
	 * 去除多余的重复字�?
	 * 
	 * @param strOri
	 * @param ch
	 * @return
	 */
	public static String compressRepeatChar(String strOri, char ch)
	{
		strOri = strOri.trim();
		StringBuffer resultBuffer = new StringBuffer();
		char[] ori_char = strOri.toCharArray();
		boolean first = true;
		for (int i = 0; i < ori_char.length; i++)
		{
			if (!first && ori_char[i] == ch)
			{
				continue;
			}
			first = (ori_char[i] != ch);
			resultBuffer.append(ori_char[i]);
		}
		return resultBuffer.toString();
	}
	
	
	//判断有效期是否大于现在时间
//		public static boolean isValidCardExpireDate(String expireDate) {
//
//			String strYear = expireDate.substring(0, 2);
//			String strMonth = expireDate.substring(2, 4);
//			int iExpireYear = Integer.valueOf(strYear).intValue();
//			int iExpireMonth = Integer.valueOf(strMonth).intValue();
//
//			Calendar c = Calendar.getInstance();
//			// 取得系统日期:
//			int iCurYear = c.get(Calendar.YEAR)%100;
//			int iCurMonth = c.get(Calendar.MONTH);
//
//			// 有效期年份大于当前年份直接返回 true
//			if (iExpireYear >iCurYear  ) {
//				return true;
//				
//			}// 有效期年份等于当前年份
//			else if (iExpireYear==iCurYear ) {
//				// 有效期月份大于等于当前月份
//				if (iExpireMonth >= iCurMonth) {
//					return true;
//				} else {
//					return false;
//				}
//			} else {
//				return false;
//			}
//		}
}
